<?php
namespace common\service;
use common\models\AttributeValue;
use common\models\ActiveAttr;
use common\models\Order;
use common\models\GoodsAttr;
use common\models\Active;
class ActiveService extends BaseService
{
    /**
     * 活动SKU
     * $type 1 过滤库存0
     * @param $id
     * @return mixed
     */
    public static function GetAttrlist($id)
    {
        $lists = ActiveAttr::find();
        $lists->andWhere(['active_id' => $id, 'is_del' => 0]);

        $list = $lists->asArray()->all();

        if ($list) {
            $aid = explode('-', $list[0]['attribute_value_id']);
            $attrtitle = AttributeValue::find()->where(['id' => $aid])->orderBy(['id' => SORT_ASC])->asArray()->all();
            $vid = array_column($list, 'attribute_value_id');

            $newdata['thead'] = array_column($attrtitle, 'attr_name');
            $newiddata = [];
            foreach ($vid as $val) {
                $newid = explode('-', $val);
                foreach ($newid as $vid) {
                    if ($vid > 0) {
                        $newiddata[] = $vid;
                    }
                }
            }
            $newiddata = array_unique($newiddata);

            $newvaldata = AttributeValue::find()->where(['in', 'id', $newiddata])->asArray()->all();
            $valdatanew = array();
            foreach ($newvaldata as $newdats) {
                $valdatanew[$newdats['id']] = $newdats;
            }

            $attr_id = array_column($list, 'id');
            $ord_count = Order::find()->select(['goods_attr_id', 'count(id) as order_salenum'])->andWhere(['pay_status' => 1])->andWhere(['in', 'goods_attr_id', $attr_id])->groupBy('goods_attr_id')->asArray()->all();
            //将二维数组转换为一维数组
            $newordercount = [];
            foreach ($ord_count as $val) {
                $newordercount[$val['goods_attr_id']] = $val;
            }

            $data = [];
            foreach ($list as $lval) {
                $newavid = explode('-', $lval['attribute_value_id']);
                $newvalue['original_price'] = $lval['original_price'];
                $newvalue['collage_price'] = $lval['collage_price'];
                $newvalue['one_price'] = $lval['one_price'];
                $newvalue['inventory'] = $lval['inventory'];
                $newvalue['image'] = GlobalUrlService::buildCdnUrl($lval['attr_img']);
                $newvalue['attr_img'] = $lval['attr_img'];
                $newvalue['attribute_value_id'] = $lval['attribute_value_id'];
                $newvalue['active_id'] = $lval['active_id'];
                $newvalue['goods_attre_id'] = $lval['goods_attre_id'];
                $newvalue['id'] = $lval['id'];
                $namedata = array();
                $attrvaldata = array();
                foreach ($newavid as $newval) {
                    if ($newval > 0) {
                        $namedata[] = $valdatanew[$newval];
                        $attrvaldata[] = $valdatanew[$newval]['value'];
                    }

                }
                $newvalue['attr_value'] = implode(':', $attrvaldata);
                $newvalue['val'] = $namedata;
                $newvalue['order_count'] = 0;
                $item['order_count'] = 0;
                foreach ($newordercount as $key => $val) {
                    if ($newvalue['id'] == $key) {
                        $newvalue['order_count'] = $newordercount[$key]['order_salenum'];
                    }
                }
                $data[] = $newvalue;
            }
            $newdata['tbody'] = $data;
            return $newdata;
        } else{
            return array();
        }
    }


    /**
     * 删除活动返回商品库存
     * @param $id
     */
    public static function DelActive($id){
        $re = Active::findOne($id);
        $avtiveAttr = ActiveAttr::find()->where(['active_id'=>$id])->asArray()->all();
        $goodsAttr = GoodsAttr::find()->where(['goods_id'=>$re['goods_id']])->asArray()->all();

        $adata = [];
        foreach ($avtiveAttr as $aattr){
            $adata[$aattr['goods_attre_id']] = $aattr;
        }

        foreach ($goodsAttr as $battr){
            $goodsdata = GoodsAttr::findOne(['id'=>$battr['id']]);
            $goodsdata->inventory = $battr['inventory']+$adata[$battr['id']]['inventory'];
            $goodsdata->update_time = time();
            $goodsdata->save();
        }
        return true;
    }
}